/**
 * 
 */
package com.thor.palmmall.rest.elecharges;

import javax.ws.rs.QueryParam;

import org.springframework.beans.factory.annotation.Autowired;

import com.thor.commons.entity.BeanOperateInfo;
import com.thor.commons.entity.BeanToOperateInfoConverter;
import com.thor.commons.entity.OperateInfo;
import com.thor.commons.exception.ThorServiceException;
import com.thor.commons.query.QueryOrderDirection;
import com.thor.commons.query.QueryResult;
import com.thor.commons.query2.QueryDefinition2;
import com.thor.commons.util.Assert;
import com.thor.palmmall.api.elecharges.EleChargesBill;
import com.thor.palmmall.api.elecharges.EleChargesBillService;
import com.thor.palmmall.api.elecharges.PayChargesParam;
import com.thor.palmmall.api.elecharges.UserEleDate;

/**
 * @author NuoSa
 *
 * @since 1.0
 */
public class AEleChargesServiceImpl implements AEleChargesBillService {

  @Autowired
  private EleChargesBillService chargesService;

  @Override
  public APayMent createChargesBill(EleChargesBill4Create chargesBill, BeanOperateInfo operInfo)
      throws ThorServiceException {
    Assert.assertArgumentNotNull(chargesBill, "chargesBill");
    Assert.assertArgumentNotNull(operInfo, "operInfo");

    EleChargesBill billTOSave = new EleChargesBill();
    billTOSave.setAmount(chargesBill.getAmount());
    billTOSave.setMethod(chargesBill.getMethod());
    billTOSave.setUserNumber(chargesBill.getUserNumber());
    OperateInfo oper = BeanToOperateInfoConverter.getInstance().convert(operInfo);
    String uuid = chargesService.saveEleChargesBill(billTOSave, oper);
    EleChargesBill genChargesBill = chargesService.getEleChargesBill(uuid);
    // 生成支付凭证
    PayChargesParam param = new PayChargesParam();
    param.setAmount(genChargesBill.getAmount());
    param.setBillNumber(genChargesBill.getBillNumber());
    param.setMethod(genChargesBill.getMethod());
    APayMent payment = payChargesBill(param, operInfo);
    return payment;

  }

  @Override
  public UserEleDate getUserEleDate(String userNumber) throws ThorServiceException {
    Assert.assertArgumentNotNull(userNumber, "chargesBill");
    return chargesService.getEleDateByUserId(userNumber);
  }

  @Override
  public QueryResult<EleChargesBill> query(EleChargesBillCondition condition) {
    Assert.assertArgumentNotNull(condition, "condition");
    QueryDefinition2 definition = new QueryDefinition2();
    if (condition.getUserNumberEquals() != null) {

      definition.addCondition(EleChargesBillService.CONDITION_USERNUBER_EQUALS,
          condition.getUserNumberEquals());
    }
    definition.addOrder(EleChargesBillService.ORDER_BY_CREATE_TIME, QueryOrderDirection.desc);
    definition.setPageNumber((int) condition.getPage());
    definition.setPageSize((int) condition.getPageSize());
    return chargesService.query(definition);

  }

  @Override
  public APayMent payChargesBill(PayChargesParam param, @QueryParam("") BeanOperateInfo operInfo)
      throws ThorServiceException {
    Assert.assertArgumentNotNull(param.getAmount(), "amount");
    Assert.assertArgumentNotNull(param.getBillNumber(), "billnumber");
    Assert.assertArgumentNotNull(param.getMethod(), "methods");
    OperateInfo oper = BeanToOperateInfoConverter.getInstance().convert(operInfo);
    return chargesService.payEleChargesBill(param, oper);
  }

  @Override
  public EleChargesBill getByBillNumber(String billNumber) {
    Assert.assertArgumentNotNull(billNumber, "billNumber");
    return chargesService.getByBillNumber(billNumber);
  }

  @Override
  public void paynotify(String billNumber, String state, BeanOperateInfo beanOperInfo)
      throws ThorServiceException {
    Assert.assertArgumentNotNull(billNumber, "billNumber");
    OperateInfo operInfo = BeanToOperateInfoConverter.getInstance().convert(beanOperInfo);
    chargesService.paynotify(billNumber, state, operInfo);
  }

  @Override
  public UserEleDate queryFreezeDataAndCT(QueryHstEleDataParam param) throws ThorServiceException {
    Assert.assertArgumentNotNull(param.getUserNumber(), "userNumber");
    Assert.assertArgumentNotNull(param.getDate(), "date");
    return chargesService.queryFreezeDataAndCT(param.getUserNumber(), param.getDate());
  }

}
